<?php

$di = \application\units\Di::getInstance();

$params = [
    'id' => $di->request->params('id'),
];

$fieldText = [
    'type'   => [1 => '菜单', 2 => '节点'],
    'status' => [1 => '正常', 0 => '隐藏'],
];

$row = $di->db->find('select * from admin_resource where id=:id', [
    'id' => $params['id'],
]);

if (!$row) throw new LogicException('未找到记录');
$row['type_text']   = $fieldText['type'][$row['type']];
$row['status_text'] = $fieldText['status'][$row['status']];
$row['resource']    = $row['resource'] ?: str_replace('/', '.', $row['path']);

$resources = $di->db->query('select *,title as name from admin_resource');
$resources = $di->tree->init($resources)->getTreeArray(0);
$row['pid_text']   = implode('/', array_column($di->tree->getParents($row['id']), 'title'));

$ids   = $di->tree->getChildrenIds($params['id'], true);
$resources = $di->tree->getTreeList($resources, 'name');
$menus = [];
foreach ($resources as &$resource) {
    $resource['disabled'] = in_array($resource['id'], $ids);
    if ($resource['type'] == 1) {
        $menus[] = $resource;
    }
}

return [
    'row'   => $row,
    'menus' => $menus,
];